import Vue from 'vue'
import router from '../router'
var RestfulPlugin={};


//restful 对象
var RestfulClient = {
    options: {
        headers: {
            token: ""
        }
    },
    setHeader: function (token) {
        Vue.http.headers.common['token'] = token;
    },

    apiBaseUrl:"http://localhost:8082/ee-manager",

    /**
     * 请求错误,比如网络错误
     * @param response
     * @param failed
     */
    processError:function(response,failed){
        if(failed){
            failed(JSON.stringify(response));
            return;
        }
        console.log("error:"+JSON.stringify(response));
    },
    /**
     * 请求正确,但是服务器验证错误
     * @param errorMessage
     * @param failed
     */
    processFailed:function(errorMessage,failed){
        if(errorMessage.errorType == 1){
            Vue.prototype.$message.error(errorMessage.errorType+":登录超时,请重新登录!")
            return;
        }

        if(errorMessage.errorType == 3){
            Vue.prototype.$message.error(errorMessage.errorType+":登录超时,请重新登录!")
            router.push("/");
            return;
        }
        //if(errorMessage.errorType == "ParametersError"){
        //    alert(errorMessage.errorType+":服务器异常,请联系管理员!");
        //    return;
        //}

        if(failed){
            failed(errorMessage);
            return;
        }
        console.log("error:"+JSON.stringify(errorMessage));
    },
    processSuccess:function(response,success,failed){
        if(response.status == 200){
            if(success) {
                var responseJson = response.body;
                if(responseJson.success === null || responseJson.success || responseJson.success == 0 ){
                    success(responseJson.success);
                    return;
                }
                if(responseJson.failed){
                    RestfulClient.processFailed(responseJson.failed,failed);
                    return;
                }
                Vue.prototype.$message.error("服务器异常,请联系管理员!");
            }
        }else{
            RestfulClient.processFailed(response,failed);
        }
        console.log("error:"+JSON.stringify(response));
    },
    post:function(url,data,success,failed){
        Vue.http.post(RestfulClient.apiBaseUrl+url,data).then(response =>{
            RestfulClient.processSuccess(response,success,failed);
        },response =>{
            RestfulClient.processError(response,failed);
        });
    },
    put:function(url,data,success,failed){
        Vue.http.put(RestfulClient.apiBaseUrl+url,data).then(response =>{
            RestfulClient.processSuccess(response,success,failed);
        },response =>{
            RestfulClient.processError(response,failed);
        });
    },
    get:function(url,success,failed){
        Vue.http.get(RestfulClient.apiBaseUrl+url).then(response =>{
            RestfulClient.processSuccess(response,success,failed);
        },response =>{
            RestfulClient.processError(response,failed);
        });
    },
    delete:function(url,success,failed){
        Vue.http.delete(RestfulClient.apiBaseUrl+url).then(response =>{
            RestfulClient.processSuccess(response,success,failed);
        },response =>{
            RestfulClient.processError(response,failed);
        });
    }
};

RestfulPlugin.install = function (Vue, options) {
    Vue.prototype.$rsClient = RestfulClient;
    RestfulPlugin.rsClient = RestfulClient;
};

export default RestfulPlugin;
